package com.fh.db.oracle;

import com.fh.db.DbIndex;
import com.fh.db.DbIndexColumn;
import com.fh.db.DbSource;
import com.fh.db.SqlRunner;
import com.fh.db.base.BaseDbIndex;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiujunda
 * @Date 21/07/2020
 * @Description
 */
public class OracleDbIndex extends BaseDbIndex {
    @Override
    public DbSource getDbSource() {
        return dbSource;
    }

    public void setDbSource(DbSource dbSource) {
        this.dbSource = dbSource;
    }

    private DbSource dbSource;

    @Override
    public List<DbIndexColumn> getDbIndexColumn() {
        return (List<DbIndexColumn>) SqlRunner.preparedSqlResult(dbSource,dbSource.getSqlSet().indexColumnPreSql(),new String[]{this.getIndexName(),this.getOwner()},
                resultSet -> {
                    List<DbIndexColumn> result=new ArrayList<>();
                    try{
                        while(resultSet.next()){
                            OracleDbIndexColumn dbIndexColumn=new OracleDbIndexColumn();
                            dbIndexColumn.setColumnName(resultSet.getString("COLUMN_NAME"));
                            dbIndexColumn.setColumnPosition(resultSet.getBigDecimal("COLUMN_POSITION").intValue());
                            result.add(dbIndexColumn);
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    return result;
                });
    }
}
